package top.itpath.config.mybits;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;
import top.itpath.common.BaseModel;

import java.util.Date;

/**
 * @author zhaomengru
 * create by  2023/10/10 - 11:37
 */
@Configuration
public class MyBatisPlusAuditor implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Object originalObject = metaObject.getOriginalObject();
        if (originalObject instanceof BaseModel) {
            setFieldValByName("createBy", 1L, metaObject);
            setFieldValByName("creatTime", new Date(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object originalObject = metaObject.getOriginalObject();
        if (originalObject instanceof BaseModel) {
            setFieldValByName("updateBy", 1L, metaObject);
            setFieldValByName("updateTime", new Date(), metaObject);
        }
    }
}
